import axios from 'axios';
import hCore from "hui-core";
import { hMessage } from "h_ui"
import {setLoadingCount, getLoadingCount} from './global.js'

// 抛出 http 异常
const HttpError = (message, code) => {
  const error = new Error(message);
  error.name = 'HttpError';
  if (code != null) {
    error.code = code;
  }

  return Promise.reject(error);
};

// 创建axios实例
const service = axios.create({
  baseURL: window.LOCAL_CONFIG.API_HOME,
  timeout: 5 * 60 * 1000, // 请求超时时间
});

// request拦截器
service.interceptors.request.use(
  (config) => {
		if(config.method === 'get' && config.params) {
			// config.params.cookies_sub_sys_no = ''
			// config.params.cookies_operator_no = '1000151'
			// config.params.cookies_menu_code = 'hsfundCmcIntelligentChat'
		} else if(config.method === 'post') {
			// config.data.cookies_sub_sys_no = ''
			// config.data.cookies_operator_no = '1000151'
			// config.data.cookies_menu_code = 'hsfundCmcIntelligentChat'
		}
    return config;
  },
  (error) => {
    Promise.reject(error);
  }
);

// response 拦截器
service.interceptors.response.use(
  (response) => {
    // 如果请求的返回类型是流，则直接返回 data
    if (response.config.responseType === 'blob') {
      return response;
    }
		if (response.config.responseType === 'arraybuffer') {
      return response;
    }
		let resDataObj = response.data
    let { code, msg, return_code } = resDataObj;
    if(code == '200' || return_code === 0){
			return resDataObj;
    } else {
			hMessage.error({
				content: msg,
				duration: 3,
			})
      return HttpError(msg || '请求异常');
		}
  },
  (error) => {
    if (error.response) {
      const { status } = error.response;
      // 目前后端返回会出现有 code 无 msg
      if (status === 401) {
        // hCore.navigate('/login');
        return;
      } 
				
			hMessage.error({
				content: error?.response?.data?.msg || error.response.statusText,
				duration: 3,
			})

      return HttpError(`请求异常：${error.response.statusText}`);
    }

    if (error.request) {
      return HttpError('请求异常：无返回结果');
    }

    return HttpError(error.msg);
  }
);

const useGet = (url, params = {}, isLoading=true) => {
	// console.log('isLoading', isLoading)
	if (isLoading) {
    setLoadingCount(getLoadingCount() + 1)
  }
  // return service.get(url, {params});
	return new Promise((resolve, reject) => {
		service.get(url, {params}).then((res) => {
			if (isLoading) {
				setLoadingCount(getLoadingCount() - 1)
			}
			resolve(res)
		}).catch(res => {
			if (isLoading) {
				setLoadingCount(getLoadingCount() - 1)
			}
			reject(res)
		})
	})
};

const usePost = (url, jsonData = {}, isLoading=true) => {
	if (isLoading) {
    setLoadingCount(getLoadingCount() + 1)
  }
  // return service.post(url, jsonData);
	return new Promise((resolve, reject) => {
		service.post(url, jsonData).then((res) => {
			if (isLoading) {
				setLoadingCount(getLoadingCount() - 1)
			}
			resolve(res)
		}).catch(res => {
			if (isLoading) {
				setLoadingCount(getLoadingCount() - 1)
			}
			reject(res)
		})
	})
};

export { useGet, usePost, service };
